import 'package:app_router_demo/config/app_router.dart';
import 'package:app_router_demo/providers/auth_provider.dart';
import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

@RoutePage()
class TabPage extends StatelessWidget {
  const TabPage({super.key});

  @override
  Widget build(BuildContext context) {
    final authProvider = Provider.of<AuthProvider>(context);
    debugPrint('TabPage build: isLoggedIn = ${authProvider.isLoggedIn}');

    return AutoTabsRouter(
      routes: const [HomeRoute(), MessageRoute(), ProfileRoute()],
      builder: (context, child) {
        final tabsRouter = AutoTabsRouter.of(context);
        return Scaffold(
          appBar: AppBar(
            title: const Text('应用主页 (TabBar)'),
            actions: [
              IconButton(
                icon: const Icon(Icons.logout),
                onPressed: () {
                  authProvider.logout(); // 登出
                  // 登出后重定向到登录页
                  context.router.replace(const LoginIndexRoute());
                },
              ),
            ],
          ),
          body: child, // Tab 内容
          bottomNavigationBar: BottomNavigationBar(
            currentIndex: tabsRouter.activeIndex,
            onTap: tabsRouter.setActiveIndex,
            items: const [
              BottomNavigationBarItem(label: '首页', icon: Icon(Icons.home)),
              BottomNavigationBarItem(label: '消息', icon: Icon(Icons.message)),
              BottomNavigationBarItem(label: '我的', icon: Icon(Icons.person)),
            ],
          ),
        );
      },
    );
  }
}
